#include "Aquarium_Devices.h";

extern Aquarium_Devices aquarium_devices;

prog_char device_name_0[] PROGMEM = "Lighting Metal";
prog_char device_name_1[] PROGMEM = "Lighting T5";
prog_char device_name_2[] PROGMEM = "Circulation Pump 0";
prog_char device_name_3[] PROGMEM = "Circulation Pump 1";
prog_char device_name_4[] PROGMEM = "Skimmer";
prog_char device_name_5[] PROGMEM = "Wave Pump";
prog_char device_name_6[] PROGMEM = "Heater";
prog_char device_name_7[] PROGMEM = "Chiller Pump";
prog_char device_name_8[] PROGMEM = "Chiller";
prog_char device_name_9[] PROGMEM = "Device 10";
prog_char device_name_10[] PROGMEM = "Device 11";

PROGMEM const char *device_name_table[] = 
{   
  device_name_0,
  device_name_1,
  device_name_2,
  device_name_3,
  device_name_4,
  device_name_5,
  device_name_6,
  device_name_7,
  device_name_8,
  device_name_9,
  device_name_10
  
};

void  Device_ON_OFF(bool Val, byte DeviceNum){
  aquarium_devices.Turn_Device_ON_OFF(Val, DeviceNum);
}

Aquarium_Devices::Aquarium_Devices() :  
DevicesStatus(0) {
//  pinMode(dataPin, OUTPUT);       //Configure each IO Pin
//  pinMode(latchPin, OUTPUT);
//  pinMode(clockPin, OUTPUT);

  for(byte i = 0 ; i <  NUMBER_OF_DEVICES ; ++i ){
    aquarium_device_arr[i].init(i,devices_pin_num+i,&Device_ON_OFF );
  }

  for(byte i = 0 ; i <  NUMBER_OF_DEVICES ; ++i ){
    aquarium_device_arr[i].update();
  }
  

}

Aquarium_Device Aquarium_Devices::Get_Aquarium_Device(int index){
  return aquarium_device_arr[index];
}

bool Aquarium_Devices::Set_Aquarium_Device(int index , Aquarium_Device aquarium_device ){
  aquarium_device_arr[index] = aquarium_device;
  aquarium_device_arr[index].Save_To_EEPROM();
  aquarium_device_arr[index].update();
  return true;
}

void Aquarium_Devices::update_devices(){
  for(byte i = 0 ; i <  NUMBER_OF_DEVICES ; ++i ){
    aquarium_device_arr[i].update();
  }
}

void  Aquarium_Devices::Turn_Device_ON_OFF(bool Val, byte DeviceNum){
  int DevicesStatus_Old = DevicesStatus;
  bitWrite(DevicesStatus,DeviceNum , Val) ;
  if ( DevicesStatus != DevicesStatus_Old ){
    Update_Outputs();
  }
}

void Aquarium_Devices::Update_Outputs(){
//  digitalWrite(latchPin, LOW);          //Pull latch LOW to start sending data
//  shiftOut(dataPin, clockPin, MSBFIRST, highByte( DevicesStatus ) );         //Send the data 
//  shiftOut(dataPin, clockPin, MSBFIRST, lowByte( DevicesStatus )  );         //Send the data
//  digitalWrite(latchPin, HIGH);         //Pull latch HIGH to stop sending data

}







